package com.shortvedio.media.api;


import com.shortvedio.media.service.MediaService;
import com.shortvedio.base.model.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.UUID;

@RestController
@Api("视频接口")
@Slf4j
public class MediaVedioController {

    @Autowired
    MediaService mediaService;

    @PostMapping("upload_vedio")//上传商品文件接口
    public Result uploadVedio(MultipartFile multipartFile) throws IOException {
        //获取文件后缀
        log.info("time:{}", LocalDateTime.now());
        String originalFilename = multipartFile.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用uuid为文件命名   创建临时文件
        File minio = File.createTempFile(UUID.randomUUID().toString(), extension);
        multipartFile.transferTo(minio);
        //获取缓存中的路径
        String path = minio.getAbsolutePath();
        Result result = mediaService.uploadVedio(path);
        return result;
    }

}
